/**
 * @author 王晓伟
 * @description 定义一个在FromPanle上展现的EditGridPanel
 * 
 */
 Ext.namespace('Ext.ux.grid');
 Ext.ux.grid.FormEditGrid = Ext.extend(Ext.grid.EditorGridPanel,{
 	viewConfig: {
        forceFit: true
    },
    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
    clicksToEdit: 1,
    frame:false,
    stripeRows:true,
    resizable:false,
    name:'',
    columnsarray:new Array(),
    initComponent:function(){
    	this.addEvents(
		/**
		 * 定义一个变化的事件,这个事件的触发条件是一旦TextField组件的大小通过拉伸发生变化后就触发这个事件
		 * _textField(组件名称)
		 */
		'chang',
		/**
		 * 定义一个设计状态下选中事件,这个事件的触发条件是一旦TextField组件在设计状态下被选中后就触发这个事件
		 * _textField(组件名称)
		 */
		'designSelected'
		);
    	this.tbar=[{
	    	text:'添加',
	    	handler:function(){
	    		var _grid = this.findParentBy(function(p){
					if(p.getXType() == 'uxformeditgrid' ){
						return true;
					}
				});
				var _recordArray = new Array();
				for(var i = 0; i<_grid.columnsarray.length;i++){
					_recordArray.push({
						name:_grid.columnsarray[i].dataIndex
					});					
				};
				var rowRecord = Ext.data.Record.create(_recordArray);
				var _rowData = new rowRecord();
				for(var i = 0; i<_grid.columnsarray.length;i++){
					_rowData.data[_grid.columnsarray[i].dataIndex] = '请输入值!';
				};
				_grid.stopEditing();
	            _grid.getStore().insert(0, _rowData);
	            _grid.startEditing(0, 0);
	            for(var i=0;i<_grid.columnsarray.length;i++){	            
	            	_grid.columnsarray[i].editor = '';
	            }
	    	}
	    },{
	    	text:'删除',
	    	handler:function(){
	    		var _grid = this.findParentBy(function(p){
					if(p.getXType() == 'uxformeditgrid' ){
						return true;
					}
				});
				if(_grid.getSelectionModel().getCount()>0){
					Ext.Msg.show({
						icon: Ext.MessageBox.INFO,	
							 title:Ext.systemTitle,
	   						 msg: '是否删除？',
	   						 buttons:Ext.Msg.YESNO,
	   						 fn:function(_buttonid){
	   							if(_buttonid == 'yes'){
	   								var   _r =  _grid.getSelectionModel().getSelected();
									_grid.getStore().remove(_r);		
	   							}		 	
	   						 }
					});
				}else{
					Ext.Msg.show({
						 icon: Ext.MessageBox.WARNING,	
						 title:Ext.systemTitle,
   						 msg: '请选择一行数据。',
   						 buttons:Ext.Msg.OK
					});
				}
	    	}
	    }];
	    var _tempcolunmsarray = this.columnsarray.slice(0);
    	for(var i = 0;i<_tempcolunmsarray.length;i++){
    		_tempcolunmsarray[i].editor = new Ext.form.TextField({allowBlank: false});
    	};
    	this.columns = _tempcolunmsarray;
	    var _fieldsarray = new Array();
	    for(var i = 0;i<this.columnsarray.length;i++){
	    	_fieldsarray.push(this.columnsarray[i].dataIndex);
	    };
	    this.store = new Ext.data.SimpleStore({
			fields:_fieldsarray,
			data:new Array()
		});	
	    Ext.ux.grid.FormEditGrid.superclass.initComponent.call(this);
	    this.on('render',function(p) {
			    if(this.resizable){
			        	new Ext.Resizable(p.getEl(), {
			                handles: 'all',
			                pinned: true,
			                transparent: true,
			                resizeElement: function() {
			                    var box = this.proxy.getBox();
			                    p.updateBox(box);
			                    if (p.layout) {
			                        p.doLayout();
			                    }
			                    p.fireEvent('chang',p);
			                    return box;
			                }
			           });
			          
			           $('#'+this.id).bind('click',{components:this},function(event){
							if(event.target.nodeName == 'FORM'){
								event.data.components.fireEvent('designSelected',event.data.components);
							}								
						});
					  $('#'+this.id).css('margin-bottom','10px');
			       }						       
		}
		);
    },
    getName:function(){
		return this.name;
	}
 });
Ext.reg('uxformeditgrid',Ext.ux.grid.FormEditGrid);